package com.example.MatchGame;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

import com.example.PullGame.Constant;


/**
 * Created by 陈勇劲 on 2017/10/3.
 */

public class MatchDrawThread extends Thread{
    SurfaceHolder sh;
    MatchGameView gameView;
    public MatchDrawThread(MatchGameView gameView){
        this.gameView=gameView;
        this.sh=gameView.getHolder();
    }

    @Override
    public void run() {

        Canvas mCanvas=null;
        while (true) {
            try {
                mCanvas=sh.lockCanvas();//锁定画布

                synchronized (this.sh) {
                    if(mCanvas!=null){
                        gameView.draw(mCanvas);//在画布上画
                    }
                }
            } finally{
                try {
                    if(sh!=null){
                        sh.unlockCanvasAndPost(mCanvas);//解锁并显示画布
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(MatchConstant.DRAWSPEED);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
